package com.mit.wx.util.http;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * @author 郭文梁
 * <p>${DATE} ${TIME} Create by 郭文梁</p>
 * <p>FOR: </p>
 */
public class SSLClients {
    /**
     * Trust all TrustStrategy
     */
    private static final TrustStrategy TRUST_STRATEGY = new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            return true;
        }
    };

    public static CloseableHttpClient createHttpsClient() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(TRUST_STRATEGY).build();
            SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext);
            HttpClients.custom().setSSLSocketFactory(factory).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }
}
